import time
import unittest
from selenium.webdriver.common.by import By
from selenium import webdriver
from unit__003.unit_06.page_seach import Page_search
from unit__003.unit_06.Mysql import Mysql


class MyTestCase(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')
        self.db = Mysql(host='10.59.9.14', user='root', password='yidong666', database='a3mall')
        self.driver = webdriver.Edge()
        self.driver.maximize_window()


    def tearDown(self) -> None:
        print('end case')
        time.sleep(5)
        self.driver.quit()

    def test_A3mall_login_01(self):
        po = Page_search(self.driver)
        # 调用page_login里面的A3mall登录逻辑
        po.A3mall_login_luoji()
        # 断言1-欢迎回来，18185603008
        dy1 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print('断言1：', dy1)
        self.assertEqual('欢迎回来，18185603008', dy1)
        time.sleep(2)
        # 断言2 数据库断言 提取用户余额和数据库余额对比
        dy2 = self.driver.find_element(By.CLASS_NAME, 'color-black').text
        time.sleep(2)
        dy2s = dy2.split()
        print('断言2:', dy2s[1])
        result1 = self.db.chaxun(f'select amount from mall_users where username={"18185603008"}')
        print('数据库提取的用户余额:', result1[0][0])
        self.assertEqual(float(dy2s[1]), float(result1[0][0]))
        # 1.搜索框输入-水瓶
        po.search()
        time.sleep(2)
        # 回车事件
        po.enters()
        # 断言1-搜索结果
        dy3 = self.driver.find_element(By.XPATH, '/html/body/div[4]/a[2]').text
        print('断言3：', dy3)
        self.assertEqual('搜索结果', dy3)
        time.sleep(2)
        # 搜索列表 提取文本-性能测试-水瓶
        sp_text = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/em/a').text
        print('商品列表-提取文本：', sp_text)
        time.sleep(2)
        # 2.点击商品
        po.sahngping()
        time.sleep(2)
        # 断言2-性能测试-水瓶
        dy2 = self.driver.find_element(By.XPATH, '//*[@id="goodsForm"]/div/div[1]/span').text
        print('断言2：', dy2)
        self.assertEqual('性能测试-水瓶', dy2)
        time.sleep(2)
        # 断言3-10.00  id="sell-price-box"
        dy3 = self.driver.find_element(By.ID, 'sell-price-box').text
        print('断言3：', dy3)
        self.assertEqual('10.00', dy3)
        time.sleep(2)
        # 3.点击立即购买
        po.buy()
        time.sleep(2)
        # 断言4-确认订单
        dy4 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/a[2]').text
        print('断言4：', dy4)
        self.assertEqual('确认订单', dy4)
        time.sleep(2)
        # 断言5-性能测试-水瓶
        dy5 = self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[4]/div[1]/table/tbody/tr[2]/td[2]/a/p').text
        print('断言5：', dy5)
        self.assertEqual(sp_text, dy5)
        time.sleep(2)
        # 提取总共需要支付的价格
        order_amount = self.driver.find_element(By.ID, 'order-amount').text
        print('订单总价为：', order_amount)
        time.sleep(2)
        # 4.点击下一步
        po.next()
        time.sleep(2)
        # 断言6-您的订单支付成功
        dy6 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/span').text
        print('断言7：', dy6)
        self.assertEqual('您的订单支付成功', dy6)
        time.sleep(10)
        yue = float(dy2s[1])-float(order_amount)
        print('计算剩余余额：',yue)
        # 购买商品后 查询数据库余额
        result2 = self.db.chaxun(f'select amount from mall_users where username={"18185603008"}')
        print('数据库提取的用户余额:', result2[0][0])
        self.assertEqual(float(yue), float(result2[0][0]))



if __name__ == '__main__':
    unittest.main()
